package com.xbfinal.eduservice.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.eduservice.entity.EduTeacher;
import com.xbfinal.eduservice.entity.vo.TeacherQuery;
import com.xbfinal.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xbfinal
 * @since 2022-12-24
 * Api作用在类上
 */
@Slf4j
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {


    @Autowired
    private EduTeacherService eduTeacherService;





    /**
     * 讲师逻辑删除  1.需要配置插件  2.需要在实体类中添加注解@TableLogic
     * @param id
     * @return
     */
    @ApiOperation("根据id逻辑删除")
    @DeleteMapping("{id}")
    public Result removeTeacher(
            @ApiParam(name = "id" ,value = "讲师的id",required = true)
            @PathVariable("id") String id){

        boolean remove = eduTeacherService.removeById(id);
        if (remove){
            return Result.ok();
        }
        return Result.failed();
    }



    @ApiOperation("根据id 查找")
    @GetMapping("/{id}")
    public Result findById(
            @ApiParam(name = "id" ,value = "id" ,required = true)
            @PathVariable("id") String id){

        log.info("\n当前id值为："+id);
        EduTeacher byId = eduTeacherService.getById(id);
        if(!BeanUtil.isEmpty(byId)){
            return Result.ok().addData(byId);
        }
        return Result.failed().addMessage("查询错误");
    }


    /**
     * 查找所有讲师
     * @return
     */
    @ApiOperation("查找所有讲师")
    @GetMapping("findAll")
    public Result findAll(){

        List<EduTeacher> list = eduTeacherService.list(null);

        return  Result.ok().addData(list);
    }

    /**
     * 分页查找所有讲师
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation("分页查找所有讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageTeacher(
           @ApiParam(name = "current",value = "当前页码")
           @PathVariable("current") Long current,
           @ApiParam(name = "limit",value = "条数")
           @PathVariable("limit") Long limit){

        //创建page对象 参数1页码 参数2条数
        Page<EduTeacher> page=new Page<>(current,limit);
        //调用Service.page 方法实现分页 参数1 page对象 参数2 条件
        IPage<EduTeacher> page1 = eduTeacherService.page(page, null);

        return Result.ok().addData(page1);
    }

    /**
     * 带条件的分页查找所有讲师
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation("带条件的分页查找所有讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(
            @ApiParam(name = "current",value = "当前页码")
            @PathVariable("current") Long current,
            @ApiParam(name = "limit",value = "条数")
            @PathVariable("limit") Long limit ,
            @RequestBody(required = false) TeacherQuery teacherQuery){

        //1.创建page对象
        Page<EduTeacher> page=new Page<>(current,limit);
        //构建条件wrapper
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name=teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StrUtil.isEmpty(name)){
            //模糊查询 参数1 数据库中字段的名称  参数2 条件
            wrapper.like("name",name);
        }
        if(level!=null){
            wrapper.eq("level",level);
        }
        if (!StrUtil.isEmpty(begin)){
            //模糊查询 参数1 数据库中字段的名称  参数2 条件
            wrapper.ge("gmt_create",begin);
        }
        if (!StrUtil.isEmpty(end)){
            //模糊查询 参数1 数据库中字段的名称  参数2 条件
            wrapper.le("gmt_create",end);
        }
        //调用Service.page 方法实现分页 参数1 page对象 参数2 条件
        IPage<EduTeacher> page1 = eduTeacherService.page(page,wrapper );

        return Result.ok().addData(page1);
    }



    @ApiOperation("新增讲师")
    @PostMapping("add")
    public Result add(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody  EduTeacher eduTeacher){

        //设置id 1609448727922094082
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return Result.ok();
        }
        return  Result.failed();
    }

    @ApiOperation("讲师修改")
    @PostMapping("updataTeacher")
    public Result updataTeacher(
            @ApiParam(name = "eduTeacher" ,value = "提交的信息",required = true)
            @RequestBody EduTeacher eduTeacher){

        boolean update = eduTeacherService.updateById(eduTeacher);

        if(update){
            return Result.ok();
        }
        return  Result.failed();
    }

}